<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Basic Tcl commands</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Tcl, learn Tcl, basic commands, tutorial, programming language, Linux">
<meta name="description" content="This chapter of the Tcl tutorial describes the basic Tcl commands.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>



<h1>Basic commands in Tcl</h1>

<p>
In this part of the Tcl tutorial, we will cover some
basic Tcl commands.
</p>

<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* LargeSquare */
google_ad_slot = "5070696322";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<p>
In the first example, we will mention the <code>puts</code> command. 
The puts command is used to print messages to the console or to other
channels like a file. The command has the following syntax:
</p>

<pre>
puts ?-nonewline? ?channelId? string
</pre>

<p>
The <code>puts</code> is the command name. Optional parameters are 
specified between question marks. The <code>-nonewline</code> switch
suppresses the newline character. By default, the command puts a newline
to each message. The channelId must be an identifier for an open channel 
such as the Tcl standard input channel (stdin), the return value from an 
invocation of open or socket. It defaults to stdout, if not specified. 
Finally the string is the message to be printed. 
</p>

<pre class="code">
#!/usr/bin/tclsh

puts "This is Tcl tutorial"
puts stdout "This is Tcl tutorial"
</pre>

<p>
The <code>puts</code> command prints a message to the console. 
Both command invocations do the same thing.
</p>

<pre class="code">
#!/usr/bin/tclsh

puts [open messages w] "This is Tcl tutorial"
</pre>

<p>
Here we use the <code>puts</code> command to write to a file.
We open a file for writing using the <code>open</code> command. 
</p>

<pre>
$ cat messages 
This is Tcl tutorial
</pre>

<p>
We show the contents of the messages file created by the above
Tcl script. 
</p>

<hr class="btm">

<p>
Greeting a user. 
</p>

<pre class="code">
#!/usr/bin/tclsh

puts -nonewline "What is your name? "
flush stdout
gets stdin name
puts "Hello $name"
</pre>

<p>
In this example, we request an input from the user and 
print the input in a custom greeting.
</p>

<pre class="explanation">
puts -nonewline "What is your name? "
</pre>

<p>
The <code>-nonewline</code> option suppresses the newline. The prompt
remains on the same line. 
</p>

<pre class="explanation">
flush stdout
</pre>

<p>
The output is buffered. To see the output immediately after the command
runs, we can use the <code>flush</code> command. The <code>stdout</code>
is the standard output. In our case a terminal. It is called a channel id
in Tcl.
</p>

<pre class="explanation">
gets stdin name
</pre>

<p>
The <code>gets</code> command reads a line from the standard input. The
result is stored in the name variable. 
</p>

<pre class="explanation">
puts "Hello $name"
</pre>

<p>
Finally, we greet the user. 
</p>

<pre>
$ ./name.tcl 
What is your name? Jan
Hello Jan
</pre>

<p>
Running the example.
</p>


<hr class="btm">

<p>
The <code>info</code> command returns information about the state
of the Tcl interpreter. 
</p>

<pre class="code">
#!/usr/bin/tclsh

puts [info tclversion]
puts [info host]
puts [info exists var]
</pre>

<p>
The info command has several options. We show three of them.
</p>

<pre class="explanation">
puts [info tclversion]
</pre>

<p>
Here we print the version of the Tcl interpreter.
</p>

<pre class="explanation">
puts [info host]
</pre>

<p>
This line prints the host name. 
</p>


<pre class="explanation">
puts [info exists var]
</pre>

<p>
Finally we check if the variable var is set.
</p>

<hr class="btm">

<p>
The <code>set</code> command is used to create and read variables.
The <code>unset</code> command destroys a variable.
</p>

<pre class="code">
#!/usr/bin/tclsh

set x 23
puts $x
puts [set x]

unset x
puts [info exists x]
</pre>

<p>
An example showing the <code>set</code> and <code>unset</code> commands. 
</p>

<pre class="explanation">
set x 23
</pre>

<p>
We create an x variable and assign a value 23 to it. 
</p>

<pre class="explanation">
puts $x
</pre>

<p>
We print the value of the x variable. 
</p>

<pre class="explanation">
puts [set x]
</pre>

<p>
This line also prints the value of the x variable. 
The <code>set</code> command with one parameter reads the value
of the variable. The value is passed to the <code>puts</code>
command and printed to the terminal.
</p>

<pre class="explanation">
unset x
</pre>

<p>
The variable x is destroyed. 
</p>

<pre class="explanation">
puts [info exists x]
</pre>

<p>
We verify the existence of the variable using the <code>info exists</code>
command.
</p>

<hr class="btm">

<p>
Tcl scripts like any other scripts can take command line arguments. 
Tcl has three predefined variables. 
</p>

<ul>
  <li>$argc - the number of arguments passed to the script</li>
  <li>$argv - the list of arguments</li>
  <li>$argv0 - the name of the script</li>
</ul>

<pre class="code">
#!/usr/bin/tclsh

puts "The script has $argc arguments"
puts "The list of arguments: $argv"
puts "The name of the script is $argv0"
</pre>

<p>
We use all the predefined variables in this script.
</p>

<pre>
$ ./args.tcl 1 2 3 
The script has 3 arguments
The list of arguments: 1 2 3
The name of the script is ./args.tcl
</pre>

<p>
Running the example.
</p>


<p>
This chapter covered some basics of the Tcl language.
</p>

<br>
<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified April 5, 2011  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

